package com.sangcomz.fishbun.util;

import ohos.app.Context;
import ohos.media.photokit.common.AVLoggerConnectionClient;
import ohos.media.photokit.metadata.AVLoggerConnection;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.IOException;

public class SingleMediaScanner implements AVLoggerConnectionClient {

    private AVLoggerConnection avLoggerConnection;
    private SingleMediaScannerCallback finishCallback;
    private File file;

    public SingleMediaScanner(Context context, File file, SingleMediaScannerCallback finishCallback) {
        this.finishCallback = finishCallback;
        this.file = file;
        avLoggerConnection = new AVLoggerConnection(context, this);
        avLoggerConnection.connect();
    }

    @Override
    public void onLoggerConnected() {
        if (file != null) {
            try {
                avLoggerConnection.performLoggerFile(file.getCanonicalPath(), null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onLogCompleted(String s, Uri uri) {
        if (finishCallback != null) {
            finishCallback.onScanCompleted();
        }
        avLoggerConnection.disconnect();
    }

    public interface SingleMediaScannerCallback {
        void onScanCompleted();
    }


}
